package com.visionblue.business.ship.page.orders

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.flyco.tablayout.SegmentTabLayout
import com.flyco.tablayout.listener.OnTabSelectListener
import com.visionblue.business.ship.R
import com.visionblue.business.ship.base.BaseActivity
import com.visionblue.business.ship.base.BaseVMActivity
import com.visionblue.business.ship.base.DefaultObserver
import kotlinx.android.synthetic.main.activity_orders.*
import org.koin.androidx.viewmodel.ext.android.getViewModel


/**
 * @author: xlh
 * @date: 2020-06-08 7:57 AM
 * @desc:
 */
class OrderActivity : BaseActivity() {

    private var mViewPager: ViewPager? = null
    private var segmentTabLayout:SegmentTabLayout?=null
    private val mTitles = arrayOf("接收联单", "申报单")
    private var mFragments: ArrayList<Fragment>? = null

    override val layoutId: Int
        get() = R.layout.activity_orders


    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        tv_ShipName.text = intent.getStringExtra("shipName")
        fl_back.setOnClickListener {
            finish()
        }

        segmentTabLayout = findViewById(R.id.stl_tabLayout)
        segmentTabLayout!!.setTabData(mTitles)

        mViewPager = findViewById(R.id.myViewPager)

        mFragments = ArrayList()
        mFragments!!.add(
            OrdersReceiveFragment()
        )
        mFragments!!.add(
           OrdersApplyFragment()
        //PdfViewFragment()
        )


        val mViewPagerAdapter = MyPagerAdapter(supportFragmentManager, mFragments!!)

        myViewPager.adapter = mViewPagerAdapter

        //滑动、点击切换页面
        stl_tabLayout.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                myViewPager.currentItem = position
            }

            override fun onTabReselect(position: Int) {}
        })



        //滑动、点击切换页面
        myViewPager.addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(i: Int, v: Float, i1: Int) {}
            override fun onPageSelected(i: Int) {
                stl_tabLayout.currentTab = i
            }

            override fun onPageScrollStateChanged(i: Int) {}
        })


    }


    companion object {
        fun start(context: Context,shipName: String) {
            Intent(context, OrderActivity::class.java)
                .putExtra("shipName", shipName)
                .run {
                    context.startActivity(this)
                }
        }
    }


    class MyPagerAdapter(fm: FragmentManager, var fragments: ArrayList<Fragment>) : FragmentPagerAdapter(fm) {
        override fun getItem(position: Int): Fragment {
            return fragments[position]
        }

        override fun getCount(): Int {
            return fragments.size
        }
    }

}